<?php

namespace app\db\index;

use \app\db\D_module as D_module_base;

class D_module extends D_module_base
{
    /**
     * 信息格式化
     */
    protected function detailed($Result, $format=1)
    {
        $Result["linkUrl"]=moduleLinkUrl($Result);
        // 返回结果
        return $Result;
    }

    /**
     * 所有数据
     * @params string 0 id
     * @params int 0 isLink
     * @params string 0 module
     * @params string 0 field
     * @param string $paramObj
     */
    public function allData($paramObj=""){
        $dataObj=paramsObj();
        $dataObj->request=paramsObj();
        $dataObj->data=paramsObj();
        $dataObj->data->use_trans=false;
        $dataObj->result=array();

        $final_arr=logInit();
        if($final_arr["error_no"]==0){
            $mixed=parseToArr($paramObj,"mixed",0);
            $params=parseToArr($paramObj,"params",array());
            $messages=parseToArr($paramObj,"messages",array());
            $rules=array(
                'id' => '',
                'field' => '',
                'module' => '',
                'isLink' => '',
            );
            $dataDefault=array();
            $dataDefault["field"]="*";
            $paramsObj=paramsObj();
            $paramsObj->params=$params;
            $paramsObj->mixed=$mixed;
            $paramsObj->rules=$rules;
            $paramsObj->messages=$messages;
            $paramsObj->dataDefault=$dataDefault;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if($checkResult["error_no"]==0){
                $dataObj->request=$checkResult["result"];
            }else{
                $final_arr=logCallErrorMsg($final_arr,$checkResult);
            }
        }
        if($final_arr["error_no"]==0){
            $condition = array();
            $condition[] = array(
                "disabled", "=", "1"
            );
            $condition[] = array(
                "isMenu", "=", "1"
            );
            if($dataObj->request->module){
                $condition[] = array(
                    "module", "=", $dataObj->request->module
                );
            }
            if($dataObj->request->id){
                $condition[] = array(
                    "id", "in", explode(",",$dataObj->request->id)
                );
            }
            if(is_numeric($dataObj->request->isLink)){
                $condition[] = array(
                    "isLink", "=", $dataObj->request->isLink
                );
            }
            try{
                $result=Db()->name(app("cmsTable")::module())->field($dataObj->request->field)->where($condition)->order("sort","asc")->select();
                $return=array();
                if($result){
                    foreach($result as  $key=>$val){
                        $return[]=$this->detailed($val);
                    }
                }
                $final_arr["result"]=$return;
            }catch (\Throwable $e){
                $final_arr=recordLogMsgLevelTwo(app("errorLevel")::E_ERROR,app("errorCode")::SQL_ERROR,lang("sys.error"),$e->getMessage());
            }
        }
        return $final_arr;
    }

    /**
     * 获取指定类型的模块
     * @param string $paramObj
     */
    public function getDataByType($paramObj=""){
        $dataObj=paramsObj();
        $dataObj->request=paramsObj();
        $dataObj->data=paramsObj();
        $dataObj->data->use_trans=false;
        $dataObj->result=array();

        $final_arr=logInit();
        if($final_arr["error_no"]==0){
            $mixed=parseToArr($paramObj,"mixed",0);
            $params=parseToArr($paramObj,"params",array());
            $messages=parseToArr($paramObj,"messages",array());
            $rules=array(
                'module' => 'require',
            );
            $dataDefault=array();
            $paramsObj=paramsObj();
            $paramsObj->params=$params;
            $paramsObj->mixed=$mixed;
            $paramsObj->rules=$rules;
            $paramsObj->messages=$messages;
            $paramsObj->dataDefault=$dataDefault;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if($checkResult["error_no"]==0){
                $dataObj->request=$checkResult["result"];
            }else{
                $final_arr=logCallErrorMsg($final_arr,$checkResult);
            }
        }
        if($final_arr["error_no"]==0){
            $condition = array();
            $condition[] = array(
                "disabled", "=", "1"
            );
            $condition[] = array(
                "module", "=", $dataObj->request->module
            );
            try{
                $result=Db()->name(app("cmsTable")::module())->field("id")->where($condition)->select();
                if($result){
                    $result=array_column($result,"id");
                }
                $final_arr["result"]=$result;
            }catch (\Throwable $e){
                $final_arr=recordLogMsgLevelTwo(app("errorLevel")::E_ERROR,app("errorCode")::SQL_ERROR,lang("sys.error"),$e->getMessage());
            }
        }
        return $final_arr;
    }

    public function __call($name, $arguments)
    {
        return recordLogMsgLevelTwo(app("errorLevel")::E_WARNING, app("errorCode")::UNDEFINED_METHOD, lang("sys.noMethodDefined"), $name, $arguments);
    }
}